//
//  UserLoginViewController.m
//  ZzzzyManProject
//
//  Created by mac on 16/3/5.
//  Copyright © 2016年 ZzzzyMan@. All rights reserved.
//

#import "UserLoginViewController.h"
#import "UITextField+Login.h"
#import "UserInfo.h"
#import <Masonry.h>
#import <AVQuery.h>
#import <AFNetworking.h>
#import "ZyProgressHUD.h"
#import "ZyProgressHUD.h"


@interface UserLoginViewController ()

@property (nonatomic,strong) UITextField * userName; //用户名
@property (nonatomic,strong) UITextField * passWord; //密码
@property (nonatomic,strong) UIButton * loginBtn;    //登陆按钮
@property (nonatomic,strong) UIButton * registerBtn; //注册按钮
@property (nonatomic,strong) UIView * loginView; //登陆界面
@property (nonatomic,strong) UIButton * backBtn; //返回按钮

@end

@implementation UserLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    
    [self setupUI];
}



#pragma mark - 设置UI控件
- (void)setupUI {
    [self.view addSubview:self.loginView];
    [self.view addSubview:self.backBtn];
    [self.backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.loginView addSubview:self.userName];
    [self.loginView addSubview:self.passWord];
    [self.loginView addSubview:self.loginBtn];
    [self.loginView addSubview:self.registerBtn];
    
    // 自动布局
    [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(300));
        make.height.equalTo(@(350));
        make.centerX.centerY.equalTo(self.view);
    }];
    
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(200));
        make.height.equalTo(@(40));
        make.centerX.equalTo(self.loginView);
        make.top.equalTo(self.loginView.mas_top).offset(95);
    }];
    
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(200));
        make.height.equalTo(@(40));
        make.centerX.equalTo(self.userName);
        make.top.equalTo(self.userName.mas_bottom).offset(10);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(80));
        make.height.equalTo(@(35));
        make.top.equalTo(self.passWord.mas_bottom).offset(15);
        make.left.equalTo(self.passWord.mas_left).offset(10);
    }];
    
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(80));
        make.height.equalTo(@(35));
        make.top.equalTo(self.passWord.mas_bottom).offset(15);
        make.right.equalTo(self.passWord.mas_right).offset(-10);
    }];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(@(25));
        make.bottom.equalTo(self.view.mas_bottom).offset(-37.5);
        make.left.equalTo(self.view.mas_left).offset(37.5);
    }];
}

#pragma mark - Btn点击事件
- (void)backBtnClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loginBtnClick {
    
#pragma mark - 检查网络状态
        AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
        [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            if (status == AFNetworkReachabilityStatusUnknown ||
                status == AFNetworkReachabilityStatusNotReachable) {
                //无网络提示
                ZyProgressHUD * hud = [ZyProgressHUD sharedHUD];
                hud.labelText = @"请检查网络";
                [self.view addSubview:hud];
                [hud showAnimated:YES whileExecutingBlock:^{
                    sleep(2);
                } completionBlock:^{
                    [hud removeFromSuperview];
                }];
            }
            else {
                //判断用户名密码是否存在数据库中
                NSString * userName = self.userName.text;
                NSString * passWord = self.passWord.text;
                NSString * cql = [NSString stringWithFormat:@"select * from UserInfo where userName = '%@' and passWord = '%@'",userName,passWord];
                [AVQuery doCloudQueryInBackgroundWithCQL:cql callback:^(AVCloudQueryResult *result, NSError *error)
                 {
                     AVObject * ob = [result.results lastObject];
                     if(ob) {
                         UserInfo * info = [[UserInfo alloc] init];
                         info.name = ob[@"name"];
                         info.userName = ob[@"userName"];
                         info.passWord = ob[@"passWord"];
                         info.iconUrl = ob[@"iconUrl"];
                         info.isPush = ob[@"isPush"];
                         
                         //归档
                         [info savaUserInfo];
                         
                         //登陆完成后更新个人信息界面
                         [[NSNotificationCenter defaultCenter] postNotificationName:@"updataName" object:info];
                         [[NSNotificationCenter defaultCenter] postNotificationName:@"loginVC" object:nil];
                          [self dismissViewControllerAnimated:YES completion:nil];
                     }
                     else {
                         ZyProgressHUD * hud = [ZyProgressHUD sharedHUD];
                         hud.labelText = @"账号密码错误";
                         [self.view addSubview:hud];
                         [hud showAnimated:YES whileExecutingBlock:^{
                             sleep(2);
                         } completionBlock:^{
                             [hud removeFromSuperview];
                         }];
                     }
                 }];
               
            }
            
        }];
        
        [manager startMonitoring];
    
    

    
    
}

#pragma mark - 懒加载子控件

- (UIView *)loginView {
    if (!_loginView) {
        _loginView = [[UIView alloc] init];
        _loginView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loginBgView"]];
    }
    return _loginView;
}

- (UITextField *)userName {
    if (!_userName) {
        _userName = [UITextField loginTextFieldWith:@"请输入用户名" andImageName:@"userName"];
        _userName.text = @"yzy930828";
    }
    return _userName;
}

- (UITextField *)passWord {
    if (!_passWord) {
        _passWord = [UITextField loginTextFieldWith:@"请输入密码" andImageName:@"userName"];
        _passWord.text = @"111111";
    }
    return _passWord;
}

- (UIButton *)loginBtn {
    if(!_loginBtn){
        _loginBtn = [[UIButton alloc] init];
        _loginBtn.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loginBtn"]];
    }
    return _loginBtn;
}

- (UIButton *)registerBtn {
    if(!_registerBtn){
        _registerBtn = [[UIButton alloc] init];
        _registerBtn.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"register"]];
    }
    return _registerBtn;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] init];
        _backBtn.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backBtn"]];
    }
    return _backBtn;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.userName resignFirstResponder];
    [self.passWord resignFirstResponder];
}


@end
